/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stealth;

import Actors.ActorManager;
import Actors.PlayerActor;
import GUI.GUI;
import ScreenLayouts.MainMenuLayout;
import ScreenLayouts.ScoresLayout;
import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import graphics.AbstractRenderer;
import graphics.RendererFactory;
import input.KeyMaps;
import java.applet.AudioClip;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import levels.LevelManager;
import resources.images.ResourceStub;
import resources.properties.Scores;
import resources.sounds.SoundStub;

/**
 *
 * @author Joshua Mabrey
 */
public class StealthGame extends Game {

    AbstractRenderer renderer;
    EnvironmentInformation envInfo;
    PlayerActor player1, player2;
    GUI gui;
    AudioClip background;

    @Override
    public void initResources() {
        //setFPS(60);
        //hideCursor();
        distribute = true;
        envInfo = new EnvironmentInformation();
        renderer = RendererFactory.getRenderer();

        //setup screen bounds for playeractors
        Polygon screen = new Polygon();
        screen.addPoint(0, 0);
        screen.addPoint(getWidth(), getHeight());
        ActorManager.addBoundingShape(screen, true);

        //add GUI components
        gui = new GUI();
        //lay the main menu bar atop the screen
        gui.addItems(new MainMenuLayout());
        GameState.setState(GameState.MENU_STATE);
        background = GameLoader.newAudioClip(SoundStub.class.getResource("ambient.wav"));
        background.loop();
    }

    public void actOnStateChange() {
        switch (GameState.getState()) {
            case GameState.MENU_STATE:
                try {
                    renderer.setBackgroundImage(ImageIO.read(ResourceStub.class.getResource("menu_back.jpg")));
                    gui.clear();
                    gui.addItems(new MainMenuLayout());
                } catch (IOException ex) {
                    System.err.println("Could not load file.");
                    System.exit(1);
                }
                break;
            case GameState.NAME_CHANGE_STATE:
                try {
                    renderer.setBackgroundImage(ImageIO.read(ResourceStub.class.getResource("menu_back.jpg")));
                    gui.clear();
                    gui.addItems(new MainMenuLayout());
                } catch (IOException ex) {
                    System.err.println("Could not load file.");
                    System.exit(1);
                }
                Scores.getScores().name = JOptionPane.showInputDialog("What is your name for the high scores?");
                Main.gameLoader.requestFocusInWindow();
                break;
            case GameState.SCORE_STATE:
                try {
                    renderer.setBackgroundImage(ImageIO.read(ResourceStub.class.getResource("menu_back.jpg")));
                    gui.clear();
                    gui.addItems(new MainMenuLayout());
                    gui.addItems(new ScoresLayout());
                } catch (IOException ex) {
                    System.err.println("Could not load file.");
                    System.exit(1);
                }
                break;
            case GameState.ONE_PLAYER_GAME_STATE:
                gui.clear();
                gui.addItems(new MainMenuLayout());
                player1 = ActorManager.getPlayer(KeyMaps.PLAYER_ONE_UP, KeyMaps.PLAYER_ONE_DOWN,
                        KeyMaps.PLAYER_ONE_LEFT, KeyMaps.PLAYER_ONE_RIGHT);
                /*player2 = ActorManager.getPlayer(KeyMaps.PLAYER_TWO_UP, KeyMaps.PLAYER_TWO_DOWN,
                KeyMaps.PLAYER_TWO_LEFT, KeyMaps.PLAYER_TWO_RIGHT);*/
                LevelManager.loadLevel(renderer, null, (short) 0);

        }
    }

    @Override
    public void update(long l) {
        //playSound("C:\\WINDOWS\\Media\\chimes.wav");
        gui.update();//always update the gui

        switch (GameState.getState()) {
            case GameState.MENU_STATE:
                break;
            case GameState.ONE_PLAYER_GAME_STATE:
                player1.act();
                LevelManager.currentLevel.update();
                //player2.act();
                break;
        }
        ActorManager.update();

    }

    @Override
    public void render(Graphics2D gd) {
        renderer.addRenderInstruction(gui.render());//always render the gui
        switch (GameState.getState()) {
            case GameState.MENU_STATE:
                break;
            case GameState.ONE_PLAYER_GAME_STATE:
                LevelManager.handOff(renderer);
                renderer.addRenderInstruction(player1.render());
                //renderer.addRenderInstruction(player2.render());
                break;
            case GameState.NAME_CHANGE_STATE:
                break;
        }
        renderer.renderAll(gd);
        while (!renderer.finishedRendering()) {
        }
        //System.out.println("FPS: " + this.getCurrentFPS());
        return;
    }

    /**
     * This method is designed to return whether or not any operations should
     * be completed by any interested caller. For instance, if the application
     * is not visible graphically, then no processing should be done.
     * Currently does not work, and SHOULD NOT be relied on.
     * @return 
     */
    public boolean isActive() {
        return bsGraphics.getBackBuffer() != null;
    }

    public EnvironmentInformation getEnvInfo() {
        return this.envInfo;
    }
}
